package ezXpns;

import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.Vector;

/**
 * 
 * @author A0086894H 
 * This class exports BudgetChange data to CSV file
 * 
 */

// @author A0086894H
public class ExportCsv {
	/**
	 * exports given expense list to a CSV file
	 * 
	 * @param sFileName
	 *            : the writepath for CSV file. for desktop:
	 *            System.getProperty("user.home")+"\\Desktop\\filename.csv"
	 * @param expenseList
	 *            : list of expense items
	 */
	public static void exportExpense(String sFileName, Iterable<Entry<UUID, BudgetChange>> expenseList) {
		try {
			FileWriter writer = new FileWriter(sFileName);
			// TODO include user
			writer.append("User:");
			writer.append('\n');
			// TODO include date
			writer.append("Exported on:");
			writer.append('\n');
			writer.append("EXPENSE ONLY");
			writer.append('\n');

			// Expenses header
			writer.append("UUID");
			writer.append(',');
			writer.append("Purchase Date");
			writer.append(',');
			writer.append("Item Name");
			writer.append(',');
			writer.append("Item Price $");
			writer.append(',');
			writer.append("Payment Type");
			writer.append(',');
			writer.append("Tags");
			writer.append("\n");

			// appends all remaining entries down the list
			for (Entry<UUID, BudgetChange> expenseEntry : expenseList) {
				String UUID = expenseEntry.getKey().toString();
				BudgetChange expenseItem = expenseEntry.getValue();
				String date = expenseItem.getDate().toString();
				String name = expenseItem.getName();
				String amount = String.valueOf(expenseItem.getAmount());
				String payment = expenseItem.getType();
				String tags = expenseItem.getTagsAsString();
				writer.append(UUID + "," + date + "," + name + "," + amount + "," + payment + "," + tags + "\n");
			}

			// writes the CSV file
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * exports given income list to a CSV file
	 * 
	 * @param sFileName
	 *            : the writepath for CSV file.
	 *            System.getProperty("user.home")+"\\Desktop\\filename.csv" for
	 *            desktop
	 * @param incomeList
	 *            : list of income items
	 */
	public static void exportIncome(String sFileName, Iterable<Entry<UUID, BudgetChange>> incomeList) {
		try {
			FileWriter writer = new FileWriter(sFileName);
			// TODO include user
			writer.append("User:");
			writer.append('\n');
			// TODO include date
			writer.append("Exported on:");
			writer.append('\n');
			writer.append("INCOME ONLY");
			writer.append('\n');

			// Income header
			writer.append("UUID");
			writer.append(',');
			writer.append("Input Date");
			writer.append(',');
			writer.append("Income Name");
			writer.append(',');
			writer.append("Income amount $");
			writer.append(',');
			writer.append("Frequency");
			writer.append(',');
			writer.append("Tags");
			writer.append('\n');

			// appends all remaining entries down the list
			for (Entry<UUID, BudgetChange> incomeEntry : incomeList) {
				String UUID = incomeEntry.getKey().toString();
				BudgetChange incomeItem = incomeEntry.getValue();
				String date = incomeItem.getDate().toString();
				String name = incomeItem.getName();
				String amount = String.valueOf(incomeItem.getAmount());
				String frequency = incomeItem.getType();
				String tags = incomeItem.getTagsAsString();
				writer.append(UUID + "," + date + "," + name + "," + amount + "," + frequency + "," + tags + "\n");
			}

			// writes the CSV file
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * exports given income list to a CSV file
	 * 
	 * @param sFileName
	 *            : the writepath for CSV file.
	 *            System.getProperty("user.home")+"\\Desktop\\filename.csv" for
	 *            desktop
	 * @param incomeList
	 *            : list of income items
	 */
	public static void exportBoth(String sFileName, Iterable<Entry<UUID, BudgetChange>> expList,
			Iterable<Entry<UUID, BudgetChange>> incList) {
		Vector<Entry<UUID, BudgetChange>> expenseList = (Vector<Entry<UUID, BudgetChange>>) expList;
		Vector<Entry<UUID, BudgetChange>> incomeList = (Vector<Entry<UUID, BudgetChange>>) incList;
		int expenseListSize = expenseList.size();
		int incomeListSize = incomeList.size();
		int maxSize = Math.max(expenseListSize, incomeListSize);

		// the following code block writes the string contents for the CSV file
		try {
			FileWriter writer = new FileWriter(sFileName);
			// header column titles
			// TODO include user
			writer.append("User:");
			writer.append('\n');
			// TODO include date
			writer.append("Exported on:");
			writer.append('\n');
			writer.append("EXPENSES");
			writer.append(",,,,,,");
			writer.append("INCOME");
			writer.append('\n');

			// Expenses header
			writer.append("UUID");
			writer.append(',');
			writer.append("Purchase Date");
			writer.append(',');
			writer.append("Item Name");
			writer.append(',');
			writer.append("Item Price $");
			writer.append(',');
			writer.append("Payment Type");
			writer.append(',');
			writer.append("Tags");
			writer.append(",,");

			// Income header
			writer.append("UUID");
			writer.append(',');
			writer.append("Input Date");
			writer.append(',');
			writer.append("Income Name");
			writer.append(',');
			writer.append("Income amount $");
			writer.append(',');
			writer.append("Frequency");
			writer.append(',');
			writer.append("Tags");
			writer.append('\n');

			// for all entries
			for (int lineIndex = 0; lineIndex < maxSize; lineIndex++) {
				// for Expense Entries
				if (lineIndex < expenseListSize) {
					String UUID = expenseList.get(lineIndex).getKey().toString();
					BudgetChange current = expenseList.get(lineIndex).getValue();
					String date = current.getDate().toString();
					String name = current.getName();
					String amount = String.valueOf(current.getAmount());
					String payment = current.getType();
					String tags = current.getTagsAsString();
					writer.append(UUID + "," + date + "," + name + "," + amount + "," + payment + "," + tags + ",,");
				} else {
					writer.append(",,,,,,");
				}

				// for Income Entries
				if (lineIndex < incomeListSize) {
					String UUID = incomeList.get(lineIndex).getKey().toString();
					BudgetChange current = incomeList.get(lineIndex).getValue();
					String date = current.getDate().toString();
					String name = current.getName();
					String amount = String.valueOf(current.getAmount());
					String frequency = current.getType();
					String tags = current.getTagsAsString();
					writer.append(UUID + "," + date + "," + name + "," + amount + "," + frequency + "," + tags + "\n");
				} else {
					writer.append("\n");
				}
			}
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}